import java.awt.Color;
import java.util.*;

public class Board 
{
	private SubGrid[][] board;

	public Board()
	{
		board = new SubGrid[3][3]; 
		initBoard();
	}
	
	public void initBoard()
	{
		for( int i = 0; i < board.length; i++)
		{
			for( int j = 0; j < board[i].length; j++ )
			{
				System.out.println("init");
				board[i][j] = new SubGrid(i,j);
			}
		}
	}
	
	public SubGrid get(int row, int col)
	{
		return board[row][col]; 
	}
	
	
	/**
	 * Checks if board is eligible to play on (not winner or tie)
	 * Check if location is valid
	 * If so, puts piece on board.
	 * @param piece to be placed
	 * @param loc where to be placed
	 * @return whether or not piece was placed.
	 */
	public boolean placePiece(GamePiece piece, GridLocation loc)
	{		
		SubGrid grid = get(loc.getMainRow(), loc.getMainCol());
		
		if( grid.canPlay() && grid.getOpenLocations().contains(loc) )
		{
			grid.set(loc.getSubRow(), loc.getSubCol(), piece);; 
			return true;
		}
		
		return false; 
	}
	
	public boolean checkForWinner()
	{
		boolean winner = false; 
		Color color; 
		
		//first check if won rows
		for( int i = 0; i < board.length; i++ )
		{
			winner = true; 
			
			if( board[i][0].hasWinner() )
			{
				color = board[i][0].getWinnerColor(); 
				
				for( int j = 1; j < board[i].length; j++)
				{
					if( board[i][j].hasWinner() )
					{
						if(!color.equals(board[i][j].getWinnerColor()))
						{
							winner = false; 
							break; 
						}
					}
				}
				
				if( winner )
				{
					winner = true; 
					celebrate(color);
					return true; 
				}
			}
			else
			{
				winner = false; 
			}
		}
		
		
		for( int j = 0; j < board[0].length; j++ )
		{
			winner = true; 
			
			if( board[0][j].hasWinner() )
			{
				color = board[0][j].getWinnerColor(); 
				
				for( int i = 1; i < board.length; i++)
				{
					if( board[i][j].hasWinner() )
					{
						if(!color.equals(board[i][j].getWinnerColor()))
						{
							winner = false; 
							break; 
						}
					}
				}
				
				if( winner )
				{
					winner = true; 
					celebrate(color);
					return true;
				}
			}
			else
			{
				winner = false; 
			}
		}
		
		if( board[0][0].hasWinner() )
		{
			color = board[0][0].getWinnerColor(); 
			winner = true; 
			int row = 1; 
			int col = 1; 
			
			while( row < board.length && col < board[row].length && board[row][col].hasWinner() )
			{
				if( color.equals(board[row][col].getWinnerColor()))
				{
					row++;
					col++;
					winner = true; 
				}
				else
				{
					winner = false; 
					break; 
				}
			}
			
			if( winner )
			{
				winner = true; 
				celebrate(color); 
				return true; 
			}
		}
		
		if( board[board.length][0].hasWinner() )
		{
			color = board[board.length][0].getWinnerColor(); 
			winner = true; 
			int row = board.length - 1; 
			int col = 1; 
			
			while( row < board.length && col < board[row].length && board[row][col].hasWinner() )
			{
				if( color.equals(board[row][col].getWinnerColor()))
				{
					row--;
					col++;
					winner = true; 
				}
				else
				{
					winner = false; 
					break; 
				}
			}
			
			if( winner )
			{
				winner = true; 
				celebrate(color);
				return true; 
			}
		}
		
		return false; 
		
	}
	
	
	public void celebrate(Color c)
	{
		//System.out.println("winner");
		
		for( int i = 0; i < board.length; i++ )
		{
			for( int j = 0; j < board[i].length; j++ )
			{
				board[i][j].colorBoard(c);
			}
		}
	}
	
	
	
	
}
